package ch.uzh.ifi.ddis.ds2011.a3.helper;

import java.awt.*;

import javax.swing.*;

/**
 * Utility do display data with a limited amount of values in a grid.
 * 
 * @author Cosmin Basca
 *
 */
public class ImagePixelMap extends JComponent {

	protected static final long serialVersionUID = 1L;
	protected int width = 500;
	protected int height = 500;
	protected int[] imageValues;
	protected JFrame window;
	protected boolean isPainted = false;
	private int imgw;
	private int imgh;
	
	public ImagePixelMap(String title, int imgw, int imgh) {
		window = new JFrame(title);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setBackground(new Color(140, 152, 181, 10));
		window.setBounds(30, 30, width, height);
		window.getContentPane().add(this);
		this.imgw = imgw;
		this.imgh = imgh;
	}

	/**
	 * Sets the data for display and updates the image.
	 * 
	 * @param data the data to be displayed.
	 */
	public void setData(int[] data) {
		imageValues = data;
		this.repaint();
		if(!window.isVisible()) {
			window.setVisible(true);			
		}
	}
	

	/**
	 * Displays an image if the data is available.
	 * 
	 * @note the data will be displayed as a squared image.
	 */
	public void paint(Graphics g) {
		g.clearRect(0, 0, window.getWidth(), window.getHeight());
		if (imageValues != null) {
			// determine the scaling
			int widthOfPixel = width / imgw;
			int heightOfPixel = height / imgh;
			
			
			
			//Print the pixels
			for (int i = 0; i < imageValues.length; i++) {
				/*if (imageValues[i] == 0) {
					g.setColor(new Color(140, 152, 181, 60));
				} else {
					g.setColor(new Color(63, 76, 107, 160));
				}*/
				int ix = i % imgw;
				int iy = i / imgh;
				
				int x = ix * widthOfPixel;
				int y = iy * heightOfPixel;
				
				int val = imageValues[i];
				long v = val & 0xFF;
				val = (int)v;
				g.setColor(new Color(val, val, val));
				//g.fillRect((i / (width / widthOfPixel)) * widthOfPixel,
				//		(i % (height / heightOfPixel)) * heightOfPixel,
				//		widthOfPixel, heightOfPixel);
				
				g.fillRect(x, y, widthOfPixel, heightOfPixel);
			}
			
			//draw grid
			g.setColor(new Color(20, 35, 70, 128));
			for (int i= 0; i<= width; i+=widthOfPixel) {
				g.drawLine(i, 0, i, height);
			}
			for (int i= 0; i<= height; i+=heightOfPixel) {
				g.drawLine(0, i, width, i);
			}
			
			isPainted = true;
		}
	}
	
	public boolean isNotUpdated() {
		if(!isPainted) {
			return true;
		}
		else {
			isPainted = false;
			return false;
		}
		
	}
}